package com.dk.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<String> list =new ArrayList<>();
        list.add("张三");
        list.add("张三丰");
        list.add("王强");
        list.add("张无忌");
        list.add("李元霸");
        //获取姓张的人并并且长度为3
        for (String s : list) {
            if(s.startsWith("张")&&s.length()==3){//判断以张开头的并且长度为3
                System.out.println(s);
            }
        }
        //获取姓王的人数
        int count =0;
        for (String s1 : list) {
            if(s1.startsWith("王")){
                count++;
            }
        }
        System.out.println("姓王的人数为"+count);
        //将姓李的单独拿出来放进容器中
        List<String> list1 =new ArrayList<>();
        for (String s2 : list) {
            if(s2.startsWith("李")){
                list1.add(s2);
            }
        }
        System.out.println(list1);
        //先转成流,并过滤
        list.stream().filter(x->x.startsWith("张")&&x.length()==3).forEach(System.out::println);
        long count1 = list.stream().filter(x -> x.startsWith("王")).count();
        System.out.println("姓王的人数为"+count1);                           //collect作用是转变成集合
        List<String> list2 = list.stream().filter(s -> s.startsWith("李")).collect(Collectors.toList());
        System.out.println(list2);
        list.stream().filter(w->w.startsWith("张")).limit(2).skip(1).forEach(System.out::println);
        List<String> strings = list.stream().filter(w -> w.startsWith("王")).collect(Collectors.toList());
        System.out.println(strings);
    }
}

